export class CallFrequencyLimiter {
  private timer = -1;
  private interval: number = 3000; // 3 seconds
  private dataQueue: any[] = [];
  private callback: (data: any) => void;

  constructor(callback: (data: any) => void) {
    this.callback = callback;
    this.startTimer();
  }

  // 启动定时器
  private startTimer() {
    if (this.timer) {
      clearInterval(this.timer);
    }
    this.timer = setInterval(() => {
      this.processData();
    }, this.interval);
  }

  // 处理数据
  private processData() {
    if (this.dataQueue.length > 0) {
      const data = this.dataQueue.pop(); // 取出队列中的最新数据
      this.dataQueue = []
      this.callback(data);
    }
  }

  // 将数据推入队列
  public push_data(data: any) {
    this.dataQueue.push(data);
  }

  // 清除定时器
  public stopTimer() {
    if (this.timer) {
      clearInterval(this.timer);
      this.timer = null;
    }
  }
}

